/*
 * FileName: ShowManService.java
 *
 */
package com.mrc.ak.showMan.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mrc.ak.showMan.model.LikeTimes;
import com.mrc.ak.showMan.utils.GetDirNameUtil;
import com.mrc.ak.showMan.utils.ImgCompressUtil;
import com.mrc.core.base.Constant;
import com.mrc.core.model.SysLogs;
import com.mrc.core.service.SysLogsService;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2017-1-7 下午5:13:38          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional
public class ShowManService {

    @Autowired
    private SysLogsService sysLogsService;
    @Autowired
    private LikeTiemsService likeTiemsService;

    /**
     * 压缩所有图片
     */
    public void compress() {
        StringBuilder filePath = null;
        if (this.isWindowsOS()) {
            filePath = new StringBuilder("E:\\JavaWorkSpace\\pic");
        } else {
            filePath = new StringBuilder("/home/pic");
        }
        traverseFolder(filePath.toString());
    }

    /**
     * 递归压缩所有图片
     * @param path
     */
    private void traverseFolder(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder(file2.getAbsolutePath());
                    } else {
                        if (file2.getName().startsWith("C_"))
                            continue;
                        System.out.println("文件:" + file2.getAbsolutePath());
                        ImgCompressUtil.compress(file2.getAbsolutePath(), file2.getParent() + File.separator + "C_" + file2.getName());
                        file2.delete();
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }

    /**
     * 根据账号，获得当前用户可查看剩余次数
     * @param userAccount
     * @return
     */
    public Integer getLastTimes(String userAccount) {
        //获得今天的日期
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<SysLogs> list = sysLogsService.listByAccount(userAccount, dateStr, Constant.LOGS_TYPE_SHOWMAN_SEARCH);
        return Constant.MAX_TIMES - list.size();
    }

    /**
     * 随机获得图片
     * @param name 搜索内容
     * @return
     */
    public Map<String, Object> getPic(String name) {
        Map<String, Object> re = new HashMap<String, Object>();
        //        //项目路径
        //        String projectPath = System.getProperty("user.dir");

        //服务器图片路径，目前写死
        StringBuilder filePath = null;
        if (this.isWindowsOS()) {
            filePath = new StringBuilder("E:\\JavaWorkSpace\\pic");
        } else {
            filePath = new StringBuilder("/home/pic");
        }
        name = GetDirNameUtil.getDirName(name);
        if (name == null) {
            re.put("total", 0);
            re.put("index", 0);
            return re;
        }
        filePath.append(File.separator).append(name).append(File.separator);
        File file = new File(filePath.toString());

        //对List进行随机抽取，获得一张图片返回
        String[] filelist = file.list();
        int index = new Random().nextInt(filelist.length);

        String fileAllPath = filePath.toString() + filelist[index];
        //        File readfile = new File(filePath.toString() + File.separator + filelist[index]);

        //先压缩 再 转base64返回
        //        BufferedImage bi = ImgCompressUtil.BufferedImagetoInputStream(fileAllPath, null, 300);
        //        String base64 = Base64AndImg.GetImageStr(bi);
        //        re.put("picBase64", base64);

        re.put("total", filelist.length);
        re.put("index", index + 1);
        re.put("filePath", fileAllPath);
        re.put("fileName", filelist[index]);

        LikeTimes lt = likeTiemsService.findByFolderAndFile(name, filelist[index]);

        //顶
        re.put("tread", lt == null ? "0" : lt.getTreadTimes());
        //踩
        re.put("parise", lt == null ? "0" : lt.getLikeTimes());
        return re;
    }

    /**
     *   true---是Windows操作系统
     * @return
     */
    private boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
        }
        return isWindowsOS;
    }

}
